package sis.ra.evaluation;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import sis.ra.utility.Utils;


public class baseline {
			
	public static String readSentence(String sentenceid)
	{
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";
		String thissent="";
		
		String file=sentenceid.split("-")[0].replace("_", " ")+"_wiki.segsent";
		String content=Utils.readFile(filepath+file);
//		System.out.println(content);
		if (!sentenceid.contains("-")) return null;
		String sentenceno=sentenceid.split("-")[1];
		String [] lines=content.split("\n");
		boolean find=false;
		for (int i=0;i<lines.length;i++)
		{
			if (lines[i].startsWith(sentenceno))
			{
	//			System.out.println("==>"+lines[i]);
				thissent=lines[i];
				find=true;
			}
		}
		if (!find)
		{
			System.out.println(sentenceid);
			System.out.println("-->fail"+sentenceno);
		}
		return thissent;
	}
	public static void main(String[] args) {
		
//		writeToEntities();
		
		//generate baseline DCG ranking
		HashMap <String, Double> verbs=verbProbobility.getVerbs();
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";
		File filepaths=new File (filepath);
		File[] files=filepaths.listFiles();
		int filecount=0;
		for (int i=0;i<files.length;i++)
		{
			if (!files[i].toString().contains("Amazon.com_wiki.candidate")) continue;
			String contents=Utils.readFile(files[i].toString());
			String [] lines=contents.split("\n");
			String companyname=files[i].toString().substring(66).replace("_wiki.candidate", "");
			filecount=filecount+1;
			HashMap <String, Double> candidates=new HashMap<String, Double> ();
			
			for (int m=0;m<lines.length;m++)
			{
				if (lines[m].length()<2) continue;
				String nounphrase=lines[m].split("\t")[0];
				String appears=lines[m].split("\t")[1];
				Double score=0.0000000000001;
				if (!nounphrase.equals("MP3")) continue;
				
				if (appears.contains(" "))
				{
					String [] apps=appears.split(" ");
					for (int n=0;n<apps.length;n++)
					{
						System.out.println(nounphrase+" "+apps[n]);
						String sentence=readSentence(apps[n]);
						Iterator<String> it=verbs.keySet().iterator();
						while (it.hasNext())
						{
							String verb=it.next();
							System.out.println(nounphrase+" "+verb+" "+sentence);
							if (sentence.contains(verb))
							{
								score=score+verbs.get(verb);
							}
						}
					}
				}else
				{
			//		System.out.println(nounphrase+" "+appears);
					String sentence=readSentence(appears);
					Iterator<String> it=verbs.keySet().iterator();
					while (it.hasNext())
					{
						String verb=it.next();
						if (sentence.contains(verb))
						{
							score=verbs.get(verb);
						}
					}
				}
	//			System.out.println(companyname+" "+nounphrase+" "+score);
				candidates.put(companyname+"\t"+nounphrase,score);
			}
			
			//sort by value
			ArrayList as = new ArrayList( candidates.entrySet() );
	        
	        Collections.sort( as , new Comparator() {
	            public int compare( Object o1 , Object o2 )
	            {
	                Map.Entry e1 = (Map.Entry)o1 ;
	                Map.Entry e2 = (Map.Entry)o2 ;
	                Double first = (Double)e1.getValue();
	                Double second = (Double)e2.getValue();
	                return second.compareTo( first );
	            }
	        });
	        
	        Iterator it = as.iterator();
	        while ( it.hasNext() )
	        {
	            String pair=((Map.Entry)it.next()).toString() ;
	            String terms=pair.split("=")[0];
	            String score=pair.split("=")[1];
	            System.out.println(terms+"\t"+score);
	  //          Utils.writeToFile(files[i].toString().replace(".candidate", ".score1"), terms+"\t"+score, true, true);
	        }
		}
		System.out.println(filecount);
	}
}